<?php

namespace app\common\command;


use app\common\model\Member;
use app\common\model\MemberAccount;
use Carbon\Carbon;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;

class InitMobile extends Command
{
    protected function configure()
    {
        $this->setName('InitMobile')
            ->addOption('count', null, Option::VALUE_REQUIRED, 'Mobile Count', 1)
            ->setDescription('Say Hello');
    }

    protected function execute(Input $input, Output $output)
    {
        if ($input->hasOption('count')) {
            $count = (int)$input->getOption('count');
        } else {
            $count = 1;
        }
        $members = Member::query()->select();
        foreach ($members as $member) {
            $moneyStart = Carbon::create(2023, 9, 8)->timestamp;
            $total = rand(1, 90) * 100;
            $this->insertAccount($member, $total, $moneyStart, time());
        }
//        $this->insertMember($count);
        $output->writeln("Success ");
    }

    protected function insertMember($count)
    {
        $mobileArr = $this->randomMobile($count);
        $realName = new \RealName();
        $regStart = Carbon::create(2023, 1, 1)->timestamp;
        $regEnd = Carbon::create(2023, 3, 1)->timestamp;
        $moneyEnd = Carbon::create(2023, 9, 8)->timestamp;
        foreach ($mobileArr as $mobile) {
            try {
                $regTime = rand($regStart, $regEnd);

                $data = [
                    'username' => $mobile,
                    'realname' => $realName->getName(),
                    'money_now' => rand(1, 100) * 10,
                    'avatar' => '/static/frontend/images/avatar/' . rand(1, 13) . '.jpg',
                    'password' => password_hash('123456', PASSWORD_BCRYPT),
                    'mobile' => $mobile,
                    'create_time' => $regTime
                ];
                $member = Member::create($data);

                if ($member['id']) {
                    $total = rand(200, 900) * 100;
                    $this->insertAccount($member, $total, $regTime, $moneyEnd);
                }
            } catch (\Exception $exception) {
                $this->output->writeln($exception->getMessage());
                continue;
            }

        }
    }

    protected function insertAccount($member, $totalMoney, $moneyStart, $moneyEnd)
    {
        $total = $totalMoney;
        while ($total > 0) {
            $change = rand(2, 20) * 100;
            $createTime = rand($moneyStart, $moneyEnd);
//            var_dump($member['id'] . '==' . $createTime);
            MemberAccount::insert([
                'member_id' => $member['id'],
                'money_before' => $change,
                'money_now' => 0,
                'money_change' => $change,
                'money_all' => $total,
                'money_consume' => $total,
                'money_frozen' => 0,
                'type' => MemberAccount::TypeWithdraw,
                'status' => MemberAccount::StatusSuccess,
                'create_time' => $createTime,
                'update_time' => $createTime,
            ]);
            $total = $total - $change;
        }
    }


    protected function randomMobile($n)
    {
        $tel_arr = array(
            '130', '131', '132', '133', '134', '135', '136', '137', '138', '139', '144',
            '147', '150', '151', '152', '153', '155', '156', '157', '158', '159', '176',
            '177', '178', '180', '181', '182', '183', '184', '185', '186', '187', '188', '189',
        );
        for ($i = 0; $i < $n; $i++) {
            $tmp[] = $tel_arr[array_rand($tel_arr)] . mt_rand(1000, 9999) . mt_rand(1000, 9999);
        }
        return array_unique($tmp);
    }

}